import { defineMock } from "../base";

// 用于存储软压板数据的变量
let softJumperData = [
  {
    ID: 1,
    Name: "差动保护软压板",
    Type: 0,
    Status: true,
  },
  {
    ID: 2,
    Name: "失灵保护软压板",
    Type: 0,
    Status: true,
  },
  {
    ID: 3,
    Name: "母线互联软压板",
    Type: 0,
    Status: false,
  },
  {
    ID: 6,
    Name: "母联分列软压板",
    Type: 0,
    Status: false,
  },
  {
    ID: 8,
    Name: "分段1分列软压板",
    Type: 0,
    Status: false,
  },
  {
    ID: 10,
    Name: "分段2分列软压板",
    Type: 0,
    Status: false,
  },
  {
    ID: 11,
    Name: "母联充电过流保护软压板",
    Type: 0,
    Status: false,
  },
  {
    ID: 13,
    Name: "分段1充电过流保护软压板",
    Type: 0,
    Status: false,
  },
  {
    ID: 15,
    Name: "分段2充电过流保护软压板",
    Type: 0,
    Status: false,
  },
  {
    ID: 16,
    Name: "母联非全相保护软压板",
    Type: 0,
    Status: false,
  },
  {
    ID: 18,
    Name: "分段1非全相保护软压板",
    Type: 0,
    Status: false,
  },
  {
    ID: 20,
    Name: "分段2非全相保护软压板",
    Type: 0,
    Status: false,
  },
  {
    ID: 21,
    Name: "远方投退压板软压板",
    Type: 3,
    Status: false,
  },
  {
    ID: 22,
    Name: "远方切换定值区软压板",
    Type: 2,
    Status: false,
  },
  {
    ID: 23,
    Name: "远方修改定值软压板",
    Type: 1,
    Status: false,
  },
];

export default defineMock([
  // GET uri: /api/sjumper/{cpu}   e.g. /api/sjumper/1
  {
    url: "sjumper/:cpu",
    method: ["GET"],
    body({ query, params }) {
      console.log("sjumper/:cpu", query, params);
      return softJumperData;
    },
  },
  // POST uri: /api/sjumper/{cpu}/batch-delete
  {
    url: "sjumper/:cpu/batch-delete",
    method: ["POST"],
    body({ body }) {
      // body: { ids: number[] }
      const { ids } = body;
      if (Array.isArray(ids)) {
        softJumperData = softJumperData.filter((item) => !ids.includes("" + item.ID));
      }
      return { success: true, data: softJumperData };
    },
  },
  // POST uri: /api/sjumper/{cpu}
  {
    url: "sjumper/:cpu",
    method: ["POST"],
    body({ query, body }) {
      console.log("sjumper/:cpu", query, body);
      // body: { updates: { id: number, data: Partial<{ Name: string; Type: number; Status: boolean }> }[] }
      // const { updates } = body;
      const updates = body["sjumpers"];
      if (Array.isArray(updates)) {
        updates.forEach((update) => {
          const idx = softJumperData.findIndex((item) => item.ID === update.id);
          console.log("idx", idx, "update", update);
          if (idx !== -1) {
            console.log("softJumperData[idx]", softJumperData[idx]);
            softJumperData[idx] = {
              ...softJumperData[idx],
              Status: update.Status,
            };
          }
        });
      }
      return { success: true, data: softJumperData };
    },
  },
]);
